package com.liveneo.adapter.mobile.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.google.common.collect.Lists;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmLossClaimFee;
import com.liveneo.adapter.base.entity.PrpmLossInjured;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.entity.PrpmLossObject;
import com.liveneo.adapter.base.entity.PrpmLossReassign;
import com.liveneo.adapter.base.entity.PrpmLossRemark;
import com.liveneo.adapter.base.entity.PrpmLossSurvey;
import com.liveneo.adapter.base.entity.PrpmLossThree;
import com.liveneo.adapter.base.entity.PrpmOcrPayee;
import com.liveneo.adapter.base.mapper.PrpmCaseInfoMapper;
import com.liveneo.adapter.base.mapper.PrpmLossClaimFeeMapper;
import com.liveneo.adapter.base.mapper.PrpmLossInjuredMapper;
import com.liveneo.adapter.base.mapper.PrpmLossItemMapper;
import com.liveneo.adapter.base.mapper.PrpmLossObjectMapper;
import com.liveneo.adapter.base.mapper.PrpmLossReassignMapper;
import com.liveneo.adapter.base.mapper.PrpmLossRemarkMapper;
import com.liveneo.adapter.base.mapper.PrpmLossSurveyMapper;
import com.liveneo.adapter.base.mapper.PrpmLossThreeMapper;
import com.liveneo.adapter.base.mapper.PrpmOcrPayeeMapper;
import com.liveneo.adapter.base.support.BaseServiceImpl;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.dto.AssessLossInfoDto;
import com.liveneo.adapter.mobile.model.dto.ClaimFeesInfoDto;
import com.liveneo.adapter.mobile.model.dto.LossInjuredDto;
import com.liveneo.adapter.mobile.model.dto.LossObjectDto;
import com.liveneo.adapter.mobile.model.dto.LossThreeDto;
import com.liveneo.adapter.mobile.model.dto.SurveyComInfoDto;
import com.liveneo.adapter.mobile.model.dto.SurveyFeesInfoDto;
import com.liveneo.adapter.mobile.model.dto.SurveyGaugeInfoDto;
import com.liveneo.adapter.mobile.model.dto.SurveyIdeaInfoDto;
import com.liveneo.adapter.mobile.model.dto.SurveySiteInfoDto;
import com.liveneo.adapter.mobile.model.request.AddLossItemRequest;
import com.liveneo.adapter.mobile.model.request.AssessLossItemRequest;
import com.liveneo.adapter.mobile.model.request.LossItemRequest;
import com.liveneo.adapter.mobile.model.request.LossReassignRequest;
import com.liveneo.adapter.mobile.model.request.LossRemarkRequest;
import com.liveneo.adapter.mobile.model.request.OcrPayeeInfoRequest;
import com.liveneo.adapter.mobile.model.request.SaveSurveyLossInfoRequest;
import com.liveneo.adapter.mobile.model.response.SurveyLossInfoResponse;
import com.liveneo.adapter.mobile.service.MobileCaseService;
import com.liveneo.adapter.mobile.service.MobileLossService;
import com.liveneo.adapter.mobile.service.ValidateService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.system.base.utils.AppException;
import com.liveneo.system.base.utils.DateUtils;

/**
 * 损失项服务类
 * @author wangxx
 * 
 * @since 2016-06-12
 */
@Service
public class MobileLossServiceImpl extends BaseServiceImpl implements MobileLossService {
    @Autowired
    private PrpmCaseInfoMapper prpmCaseInfoMapper;
    @Autowired
    private PrpmLossSurveyMapper prpmLossSurveyMapper;
    @Autowired
    private PrpmLossThreeMapper prpmLossThreeMapper;
    @Autowired
    private PrpmLossObjectMapper prpmLossObjectMapper;
    @Autowired
    private PrpmLossInjuredMapper prpmLossInjuredMapper;
    @Autowired
    private PrpmLossClaimFeeMapper prpmLossClaimFeeMapper;
    @Autowired
    private PrpmLossRemarkMapper prpmLossRemarkMapper;
    @Autowired
    private PrpmOcrPayeeMapper prpmOcrPayeeMapper;
    @Autowired
    private PrpmLossReassignMapper prpmLossReassignMapper;
    @Autowired
    private MobileCaseService  mobileCaseService;
    @Autowired
    private ValidateService    validateService;
    @Autowired
    private PrpmLossItemMapper prpmLossItemMapper;

    @Override
    public List<PrpmLossItem> getLossItemList(LossItemRequest request) throws AppException {
        // 校验
        validateService.validateGetLossItemList(request);
        // 返回查询结果
        return prpmLossItemMapper.getLossItemList(request);
    }

    @Override
    public List<PrpmLossItem> getAssessLossItem(AssessLossItemRequest request) throws AppException {
        // 校验
        validateService.validateGetAssessLossItem(request);
        String surveyUserCode = request.getSurveyUserCode();
        String caseNo = request.getCaseNo();
        String startDate = request.getStartDate();
        String stopDate = request.getStopDate();
        return prpmLossItemMapper.selectLossItems(surveyUserCode, caseNo, startDate, stopDate);
    }

    @Override
    public PrpmLossItem getLossItemByCaseNoLossId(String caseNo, String lossId) {
        List<PrpmLossItem> items = prpmLossItemMapper.getLossItemByCaseNoLossId(caseNo, lossId);
        return items.size() > 0 ? items.get(0) : null;
    }

    @Override
    public PrpmLossItem getLossItemByLossUuid(String lossuuid) {
        List<PrpmLossItem> items = prpmLossItemMapper.getLossItemByLossUuid(lossuuid);
        return items.size() > 0 ? items.get(0) : null;
    }

    @Override
    public List<PrpmLossItem> getLossItemListByUuids(List<String> lossUuids) {
        return prpmLossItemMapper.selectByUuids(lossUuids);
    }

//    @Override
//    public List<PrpmLossItem> getSurveyLossItem(String caseUuid) {
//        List<PrpmLossItem> lossItems = prpmLossItemMapper.getSurveyLossItem(caseUuid);
//        LogUtils.info("获取查勘任务数量：" + lossItems.size());
//        for (PrpmLossItem item : lossItems) {
//            String lossUuid = item.getLossItemId();
//            LogUtils.info("查勘任务的类型：" + item.getLossType() + ",查勘任务ID：" + item.getLossItemId());
//            // TODO 数据库增加查勘员确认状态字段后 去掉繁琐的判断
//            if ("0".equals(item.getLossType())) {
//                // 公共信息
//                SurveyComInfoDto comInfo = prpmLossSurveyMapper.selectComInfoByUuid(lossUuid);
//                if (comInfo != null && comInfo.getLicenseNo() != null) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else if ("1".equals(item.getLossType())) {
//                // 现场信息
//                SurveySiteInfoDto siteInfo = prpmLossSurveyMapper.selectSiteInfoByUuid(lossUuid);
//                if (siteInfo != null && siteInfo.getSurveyPlace() != null) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else if ("2".equals(item.getLossType())) {
//                // 查勘意见
//                SurveyIdeaInfoDto ideaInfo = prpmLossSurveyMapper.selectIdeaInfoByUuid(lossUuid);
//                if (ideaInfo != null && ideaInfo.getSurveyType() != null) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else if ("3".equals(item.getLossType())) {
//                // 三者车信息
//                List<PrpmLossThree> lossThreeList = prpmLossThreeMapper.selectByCaseUuid(caseUuid);
//                if (lossThreeList != null && lossThreeList.size() > 0) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else if ("4".equals(item.getLossType())) {
//                // 物损信息
//                List<PrpmLossObject> lossObjectList = prpmLossObjectMapper.selectByCaseUuid(caseUuid);
//                if (lossObjectList != null && lossObjectList.size() > 0) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else if ("5".equals(item.getLossType())) {
//                // 人员伤亡信息
//                List<PrpmLossInjured> injuredList = prpmLossInjuredMapper.selectByCaseUuid(caseUuid);
//                if (injuredList != null && injuredList.size() > 0) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else if ("6".equals(item.getLossType())) {
//                // 直接理赔信息
//                List<PrpmLossClaimFee> feeList = prpmLossClaimFeeMapper.selectByCaseUuid(caseUuid);
//                if (feeList != null && feeList.size() > 0) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else if ("7".equals(item.getLossType())) {
//                // 预估信息
//                SurveyGaugeInfoDto gaugeInfo = prpmLossSurveyMapper.selectGaugeInfoByUuid(lossUuid);
//                if (gaugeInfo != null && gaugeInfo.getLossTotalFees() != null) {
//                    item.setIsConfirm("1");
//                } else {
//                    item.setIsConfirm("2");
//                }
//            } else {
//                item.setIsConfirm("2");
//            }
//        }
//        return lossItems;
//    }

    @Override
    public SurveyLossInfoResponse getSurveyLossInfo(String lossUuid, String surveyItemFlag) throws Exception {
        // 校验
        if (lossUuid == null || "".equals(lossUuid)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.CODE_PARAM_NO_LOSSUUID);
        }
        if (surveyItemFlag == null || "".equals(surveyItemFlag)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.CODE_PARAM_NO_FLAG);
        }
        SurveyLossInfoResponse result = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE,null, SurveyLossInfoResponse.class);
        LogUtils.info("surveyItemFlag:" + surveyItemFlag + ", lossUuid:" + lossUuid);
        // 公共信息
        if ("0".equals(surveyItemFlag)) {
            SurveyComInfoDto comInfo = prpmLossSurveyMapper.selectComInfoByUuid(lossUuid);
            if (comInfo == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            result.setSurveyComInfo(comInfo);
        }
        // 现场信息
        if ("1".equals(surveyItemFlag)) {
            SurveySiteInfoDto siteInfo = prpmLossSurveyMapper.selectSiteInfoByUuid(lossUuid);
            if (siteInfo == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            result.setSurveySiteInfo(siteInfo);
        }
        // 查勘意见
        if ("2".equals(surveyItemFlag)) {
            SurveyIdeaInfoDto ideaInfo = prpmLossSurveyMapper.selectIdeaInfoByUuid(lossUuid);
            if (ideaInfo == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            result.setSurveyIdeaInfo(ideaInfo);
        }
        // 三者车
        if ("3".equals(surveyItemFlag)) {
            PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
            if (lossItem == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            String caseNo = lossItem.getCaseNo();
            List<PrpmLossThree> lossThreeList = prpmLossThreeMapper.selectByCaseNo(caseNo);
            List<LossThreeDto> dtos = Lists.newArrayList();
            for (PrpmLossThree lossThree : lossThreeList) {
                LossThreeDto dto = new LossThreeDto();
                BeanUtils.copyProperties(dto, lossThree);
                dto.setLossFees(lossThree.getLossFees().toPlainString());
                dtos.add(dto);
            }
            result.setThreeInfoList(dtos);
        }
        // 物损
        if ("4".equals(surveyItemFlag)) {
            PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
            if (lossItem == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            String caseNo = lossItem.getCaseNo();
            List<PrpmLossObject> lossObjectList = prpmLossObjectMapper.selectByCaseNo(caseNo);
            List<LossObjectDto> dtos = Lists.newArrayList();
            for (PrpmLossObject lossObject : lossObjectList) {
                LossObjectDto dto = new LossObjectDto();
                BeanUtils.copyProperties(dto, lossObject);
                dto.setLossFees(lossObject.getLossFees().toPlainString());
                dtos.add(dto);
            }
            result.setObjectInfoList(dtos);
        }
        // 人员伤亡
        if ("5".equals(surveyItemFlag)) {
            PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
            if (lossItem == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            String caseNo = lossItem.getCaseNo();
            List<PrpmLossInjured> injuredList = prpmLossInjuredMapper.selectByCaseNo(caseNo);
            List<LossInjuredDto> dtos = Lists.newArrayList();
            for (PrpmLossInjured injured : injuredList) {
                LossInjuredDto dto = new LossInjuredDto();
                BeanUtils.copyProperties(dto, injured);
                dto.setLossFees(injured.getLossFees().toPlainString());
                dtos.add(dto);
            }
            result.setInjuredInfoList(dtos);
        }
        // 直接理赔费用
        if ("6".equals(surveyItemFlag)) {
            PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
            if (lossItem == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            String caseNo = lossItem.getCaseNo();
            List<PrpmLossClaimFee> feeList = prpmLossClaimFeeMapper.selectByCaseNo(caseNo);
            List<SurveyFeesInfoDto> dtos = Lists.newArrayList();
            for (PrpmLossClaimFee fee : feeList) {
                SurveyFeesInfoDto dto = new SurveyFeesInfoDto();
                dto.setClaimName(fee.getFeesName());
                dto.setClaimFees(fee.getFeesNum());
                dtos.add(dto);
            }
            result.setClaimFeesList(dtos);
        }
        // 预估信息
        if ("7".equals(surveyItemFlag)) {
            SurveyGaugeInfoDto gaugeInfo = prpmLossSurveyMapper.selectGaugeInfoByUuid(lossUuid);
            if (gaugeInfo == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            result.setSurveyGaugeInfo(gaugeInfo);
        }
        return result;
    }

    @Override
    public void updateLosssItemById(PrpmLossItem prpmLossItem) {
        prpmLossItemMapper.updateByPrimaryKey(prpmLossItem);
    }

    @Override
    public List<PrpmLossItem> getLossItemByMobileLossId(String mobile, String lossId) {
        return prpmLossItemMapper.getLossItemByMobileLossId(mobile, lossId);
    }

    @Transactional
    @Override
    public void saveSurveyLossInfo(SaveSurveyLossInfoRequest request) {
        // 公共信息
        SurveyComInfoDto comInfo = request.getSurveyComInfo();
        if (comInfo != null) {
            String lossUuid = comInfo.getLossUuid();
            LogUtils.info("暂存公共信息，lossUuid:" + lossUuid);
            PrpmLossSurvey lossSurvey = prpmLossSurveyMapper.selectByLossUuid(lossUuid);
            if (lossSurvey == null) {
                lossSurvey = getLossSurvey(lossUuid);
            }
            lossSurvey.setDamageDate(DateUtils.parseDate(comInfo.getDamageDate()));
            lossSurvey.setReportDate(DateUtils.parseDate(comInfo.getReportDate()));
            lossSurvey.setLicenseNo(comInfo.getLicenseNo());
            lossSurvey.setVinNo(comInfo.getVinNo());
            lossSurvey.setEngineCode(comInfo.getEngineCode());
            lossSurvey.setPlateType(comInfo.getPlateType());
            lossSurvey.setBrandModel(comInfo.getBrandModel());
            lossSurvey.setUserPhone(comInfo.getUserPhone());
            if (lossSurvey.getId() == null) {
                prpmLossSurveyMapper.insert(lossSurvey);
            } else {
                prpmLossSurveyMapper.updateByPrimaryKey(lossSurvey);
            }
        }
        // 现场信息
        SurveySiteInfoDto siteInfo = request.getSurveySiteInfo();
        if (siteInfo != null) {
            String lossUuid = siteInfo.getLossUuid();
            LogUtils.info("暂存现场信息，lossUuid:" + lossUuid);
            PrpmLossSurvey lossSurvey = prpmLossSurveyMapper.selectByLossUuid(lossUuid);
            if (lossSurvey == null) {
                lossSurvey = getLossSurvey(lossUuid);
            }
            lossSurvey.setSurveyPlace(siteInfo.getSurveyPlace());
            lossSurvey.setSurveyDate(DateUtils.parseDate(siteInfo.getSurveyDate()));
            lossSurvey.setInformant(siteInfo.getInformant());
            lossSurvey.setInformantPhone(siteInfo.getInformantPhone());
            lossSurvey.setDamagePlace(siteInfo.getDamagePlace());
            lossSurvey.setDamageDriver(siteInfo.getDamageDriver());
            lossSurvey.setDriverLicense(siteInfo.getDriverLicense());
            lossSurvey.setPermDrive(siteInfo.getPermDrive());
            lossSurvey.setTempCar(siteInfo.getTempCar());
            lossSurvey.setFileNo(siteInfo.getFileNo());
            lossSurvey.setAccidentResponsible(siteInfo.getAccidentProportion());
            lossSurvey.setAccidentProportion(siteInfo.getAccidentProportion());
            lossSurvey.setAccidentTreatment(siteInfo.getAccidentTreatment());
            lossSurvey.setBitClaims(siteInfo.getBitClaims());
            lossSurvey.setBeClaimants(siteInfo.getBeClaimants());
            lossSurvey.setAccidentConfirm(siteInfo.getAccidentConfirm());
            lossSurvey.setCatastropheType(siteInfo.getCatastropheType());
            lossSurvey.setCatastropheCode(siteInfo.getCatastropheCode());
            lossSurvey.setSelfPay(siteInfo.getSelfPay());
            lossSurvey.setAccidentsType(siteInfo.getAccidentsType());
            lossSurvey.setLosses(siteInfo.getLosses());
            if (lossSurvey.getId() == null) {
                prpmLossSurveyMapper.insert(lossSurvey);
            } else {
                prpmLossSurveyMapper.updateByPrimaryKey(lossSurvey);
            }
        }
        // 查勘意见
        SurveyIdeaInfoDto ideaInfo = request.getSurveyIdeaInfo();
        if (ideaInfo != null) {
            String lossUuid = ideaInfo.getLossUuid();
            LogUtils.info("暂存查勘意见，lossUuid:" + lossUuid);
            PrpmLossSurvey lossSurvey = prpmLossSurveyMapper.selectByLossUuid(lossUuid);
            if (lossSurvey == null) {
                lossSurvey = getLossSurvey(lossUuid);
            }
            lossSurvey.setSurveyType(ideaInfo.getSurveyType());
            lossSurvey.setCaseType(ideaInfo.getCaseType());
            lossSurvey.setSurveyReport(ideaInfo.getSurveyReport());
            lossSurvey.setCaseCancel(ideaInfo.getCaseCancel());
            lossSurvey.setCancelType(ideaInfo.getCancelType());
            lossSurvey.setCancelReason(ideaInfo.getCancelReason());
            if (lossSurvey.getId() == null) {
                prpmLossSurveyMapper.insert(lossSurvey);
            } else {
                prpmLossSurveyMapper.updateByPrimaryKey(lossSurvey);
            }
        }
        // 预估信息
        SurveyGaugeInfoDto gaugeInfo = request.getSurveyGaugeInfo();
        if (gaugeInfo != null) {
            String lossUuid = gaugeInfo.getLossUuid();
            LogUtils.info("暂存预估信息，lossUuid:" + lossUuid);
            PrpmLossSurvey lossSurvey = prpmLossSurveyMapper.selectByLossUuid(lossUuid);
            if (lossSurvey == null) {
                lossSurvey = getLossSurvey(lossUuid);
            }
            if (gaugeInfo.getRescueFees() != null) {
                lossSurvey.setRescueFees(new BigDecimal(gaugeInfo.getRescueFees()));
            }
            if (gaugeInfo.getVehicleLossFees() != null) {
                lossSurvey.setVehicleLossFees(new BigDecimal(gaugeInfo.getVehicleLossFees()));
            }
            lossSurvey.setVehicleRisks(gaugeInfo.getVehicleRisks());
            if (gaugeInfo.getThreeLossFees() != null) {
                lossSurvey.setThreeLossFees(new BigDecimal(gaugeInfo.getThreeLossFees()));
            }
            if (gaugeInfo.getObjectLossFees() != null) {
                lossSurvey.setObjectLossFees(new BigDecimal(gaugeInfo.getObjectLossFees()));
            }
            if (gaugeInfo.getPeopleLossFees() != null) {
                lossSurvey.setPeopleLossFees(new BigDecimal(gaugeInfo.getPeopleLossFees()));
            }
            if (gaugeInfo.getLossTotalFees() != null) {
                lossSurvey.setLossTotalFees(new BigDecimal(gaugeInfo.getLossTotalFees()));
            }
            if (lossSurvey.getId() == null) {
                prpmLossSurveyMapper.insert(lossSurvey);
            } else {
                prpmLossSurveyMapper.updateByPrimaryKey(lossSurvey);
            }
        }
        // 三者车信息
        List<LossThreeDto> threeList = request.getThreeInfoList();
        if (threeList != null && threeList.size() > 0) {
            for (LossThreeDto three : threeList) {
                String lossUuid = three.getLossUuid();
                LogUtils.info("暂存三者车，lossUuid:" + lossUuid);
                PrpmLossThree lossThree = prpmLossThreeMapper.getByLossUuid(lossUuid);
                if (lossThree == null) {
                    lossThree = new PrpmLossThree();
                    PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
                    lossThree.setLossUuid(lossUuid);
                    lossThree.setCaseNo(lossItem.getCaseNo());
//                    lossThree.setCaseUuid(lossItem.getCaseUuid());
                    lossThree.setSurveyorCode(lossItem.getSurveyorCode());
                    lossThree.setSurveyorName(lossItem.getSurveyorName());
                }
                lossThree.setThLicenseNo(three.getThLicenseNo());
                lossThree.setThPlateType(three.getThPlateType());
                lossThree.setThBrandModel(three.getThBrandModel());
                lossThree.setThVinCode(three.getThVinCode());
                lossThree.setThEngineCode(three.getThEngineCode());
                lossThree.setThActingPay(three.getThActingPay());
                lossThree.setThFileNo(three.getThFileNo());
                lossThree.setThDamageDriver(three.getThDamageDriver());
                lossThree.setContact(three.getContact());
                lossThree.setContactPhone(three.getContactPhone());
                lossThree.setLossFees(new BigDecimal(three.getLossFees()));
                lossThree.setThDriverLicense(three.getThDriverLicense());
                lossThree.setThTempCar(three.getThTempCar());
                lossThree.setThRecoveryDuty(three.getThRecoveryDuty());
                lossThree.setInsCompany(three.getInsCompany());
                lossThree.setLossNotes(three.getLossNotes());
                if (lossThree.getId() == null) {
                    prpmLossThreeMapper.insert(lossThree);
                } else {
                    prpmLossThreeMapper.updateByPrimaryKey(lossThree);
                }
            }
        }
        // 物损信息列表
        List<LossObjectDto> objectList = request.getObjectInfoList();
        if (objectList != null) {
            for (LossObjectDto object : objectList) {
                String lossUuid = object.getLossUuid();
                LogUtils.info("暂存物损信息，lossUuid:" + lossUuid);
                PrpmLossObject lossObject = prpmLossObjectMapper.getByLossUuid(lossUuid);
                if (lossObject == null) {
                    lossObject = new PrpmLossObject();
                    PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
                    lossObject.setLossName(lossItem.getLossName());
                    lossObject.setLossUuid(lossUuid);
                    lossObject.setCaseNo(lossItem.getCaseNo());
//                    lossObject.setCaseUuid(lossItem.getCaseUuid());
                    lossObject.setLossType(lossItem.getLossType().toString());
                    lossObject.setSurveyorCode(lossItem.getSurveyorCode());
                    lossObject.setSurveyorName(lossItem.getSurveyorName());
                }
                lossObject.setBelongs(object.getBelongs());
                lossObject.setLossFees(new BigDecimal(object.getLossFees()));
                lossObject.setThRecoveryDuty(object.getThRecoveryDuty());
                lossObject.setInsCompany(object.getInsCompany());
                lossObject.setIfInsurance(object.getIfInsurance());
                lossObject.setPolicyNum(object.getPolicyNum());
                lossObject.setLossNum(new Byte(object.getLossNum()));
                lossObject.setContact(object.getContact());
                lossObject.setContactPhone(object.getContactPhone());
                if (lossObject.getId() == null) {
                    prpmLossObjectMapper.insert(lossObject);
                } else {
                    prpmLossObjectMapper.updateByPrimaryKey(lossObject);
                }
            }
        }
        // 人员伤亡信息
        List<LossInjuredDto> injuredList = request.getPeopleInfoList();
        if (injuredList != null) {
            for (LossInjuredDto injured : injuredList) {
                String lossUuid = injured.getLossUuid();
                LogUtils.info("暂存人员伤亡信息，lossUuid:" + lossUuid);
                PrpmLossInjured lossInjured = prpmLossInjuredMapper.getByLossUuid(lossUuid);
                if (lossInjured == null) {
                    lossInjured = new PrpmLossInjured();
                    PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
                    lossInjured.setLossName(lossItem.getLossName());
                    lossInjured.setLossUuid(lossUuid);
                    lossInjured.setCaseNo(lossItem.getCaseNo());
//                    lossInjured.setCaseUuid(lossItem.getCaseUuid());
//                    lossInjured.setLossType(lossItem.getLossType().toString());
                    lossInjured.setSurveyorCode(lossItem.getSurveyorCode());
                    lossInjured.setSurveyorName(lossItem.getSurveyorName());
                }
                lossInjured.setLossFees(new BigDecimal(injured.getLossFees()));
                lossInjured.setThRecoveryDuty(injured.getThRecoveryDuty());
                lossInjured.setSmallInjured(injured.getSmallinjured());
                lossInjured.setGender(injured.getGender());
                lossInjured.setAge(new Byte(String.valueOf(injured.getAge())));
                lossInjured.setIdCard(injured.getIdCard());
                lossInjured.setContact(injured.getContact());
                lossInjured.setContactPhone(injured.getContactPhone());
                lossInjured.setLossNotes(injured.getLossNotes());
                if (lossInjured.getId() == null) {
                    prpmLossInjuredMapper.insert(lossInjured);
                } else {
                    prpmLossInjuredMapper.updateByPrimaryKey(lossInjured);
                }
            }
        }
        // 直接理赔费用
        List<ClaimFeesInfoDto> claimFeesList = request.getClaimFeesList();
        if (claimFeesList != null) {
            for (ClaimFeesInfoDto claimFee : claimFeesList) {
                String lossUuid = claimFee.getLossUuid();
                LogUtils.info("暂存直接理赔信息，lossUuid:" + lossUuid);
                PrpmLossClaimFee fee = prpmLossClaimFeeMapper.selectByLossUuid(lossUuid);
                if (fee == null) {
                    fee = new PrpmLossClaimFee();
                    PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
                    fee.setLossUuid(lossUuid);
                    fee.setCaseNo(lossItem.getCaseNo());
//                    fee.setCaseUuid(lossItem.getCaseUuid());
                }
                fee.setFeesName(claimFee.getClaimName());
                fee.setFeesNum(claimFee.getClaimFees());
                if (fee.getId() == null) {
                    prpmLossClaimFeeMapper.insert(fee);
                } else {
                    prpmLossClaimFeeMapper.updateByPrimaryKey(fee);
                }
            }
        }
    }

    /**
     * 新建查勘信息对象， 用于查勘信息对象不存在的时候保存查勘信息
     * @param lossUuid 任务uuid
     * @return 返回查勘信息对象
     */
    private PrpmLossSurvey getLossSurvey(String lossUuid) {
        PrpmLossSurvey lossSurvey = new PrpmLossSurvey();
        PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
        lossSurvey.setLossName(lossItem.getLossName());
        lossSurvey.setLossUuid(lossUuid);
        lossSurvey.setCaseNo(lossItem.getCaseNo());
//        lossSurvey.setCaseUuid(lossItem.getCaseUuid());
//        lossSurvey.setLossType(lossItem.getLossType());
        lossSurvey.setSurveyorCode(lossItem.getSurveyorCode());
        lossSurvey.setSurveyorName(lossItem.getSurveyorName());
        return lossSurvey;
    }

    @Override
    public void addPayeeInfo(OcrPayeeInfoRequest request) throws AppException {
        // 校验
        validateService.validateAddPayeeInfo(request);
        // 领款人信息
        PrpmOcrPayee ocrPayee = new PrpmOcrPayee();
        // ocrPayee.setMobile(request.getMobile());
       // ocrPayee.setLossUuid(request.getLossUuid());
//        ocrPayee.setCaseUuid(request.getCaseUuid());
        ocrPayee.setCaseNo(request.getCaseNo());
        // ocrPayee.setPublicAndPrivate(request.getPublicAndPrivate());
        // ocrPayee.setPayeeObject(request.getPayeeObject());
        ocrPayee.setPayeeName(request.getPayeeName());
        ocrPayee.setPayeeCard(request.getPayeeCard());
        ocrPayee.setBank(request.getBank());
        ocrPayee.setBankCode(request.getBankCode());
        ocrPayee.setBranchBank(request.getBranchBank());
        ocrPayee.setBranchBankCode(request.getBranchBankCode());
        ocrPayee.setBankCard(request.getBankCard());
        ocrPayee.setCardType(request.getCardType());
        ocrPayee.setProvince(request.getProvince());
        ocrPayee.setProvinceCode(request.getProvinceCode());
        ocrPayee.setCity(request.getCity());
        ocrPayee.setCityCode(request.getCityCode());
        prpmOcrPayeeMapper.insert(ocrPayee);
    }

    @Override
    public PrpmOcrPayee getPayeeInfo(String lossUuid) {
        return prpmOcrPayeeMapper.selectByLossUuid(lossUuid);
    }

    @Override
    public void addLossRemark(LossRemarkRequest request) throws AppException {
        // 校验
        PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(request.getLossUuid());
        if (lossItem == null) {
            throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
        }
        // 保存备注信息
        PrpmLossRemark remark = new PrpmLossRemark();
        remark.setCaseNo(request.getCaseNo());
//        remark.setCaseUuid(lossItem.getCaseUuid());
        remark.setLossName(lossItem.getLossName());
        remark.setLossUuid(lossItem.getLossItemId());
//        remark.setLossType(lossItem.getLossType());
        remark.setSurveyorName(request.getSurveyName());
        remark.setSurveyorCode(request.getSurveyUserCode());
        remark.setOrgCode(request.getOrgCode());
        remark.setRemark(request.getRemark());
        prpmLossRemarkMapper.insert(remark);
    }

    @Override
    public List<PrpmLossRemark> getLossRemarkByLossUuid(String lossUuid) {
        return prpmLossRemarkMapper.selectByLossUuid(lossUuid);
    }

    @Transactional
    @Override
    public void addLossItem(AddLossItemRequest request) throws AppException {
        // 校验
        validateService.validateAddLossItem(request);
        // 调用理赔核心接口 非本人时需要通知核心调度
        // TODO 调用调度增加任务接口
        // TODO 保存任务到prpm_loss_item
        PrpmCaseInfo caseInfo = mobileCaseService.getCaseInfoByCaseNo(request.getCaseNo());
        PrpmLossItem lossItem = new PrpmLossItem();
        lossItem.setCaseNo(caseInfo.getCaseNo());
//        lossItem.setCaseUuid(caseInfo.getCaseInfoId());
        lossItem.setEndCaseTime(caseInfo.getEndCaseTime());
        lossItem.setFinishCaseTime(caseInfo.getFinishCaseTime());
        lossItem.setLossName(request.getLossName());
        lossItem.setLossType(Integer.valueOf(request.getLossType()));
    }

    @Override
    public AssessLossInfoDto getLossInfo(String lossUuid) {
        PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
        if (lossItem != null) {
            AssessLossInfoDto dto = new AssessLossInfoDto();
            dto.setCaseNo(lossItem.getCaseNo());
            dto.setContact(lossItem.getContact());
            dto.setContactPhone(lossItem.getContactPhone());
            dto.setLossStatus(lossItem.getLossStatus().toString());
            dto.setLossType(lossItem.getLossType().toString());
            PrpmCaseInfo caseInfo = prpmCaseInfoMapper.selectByCaseNo(lossItem.getCaseNo());
            if (caseInfo != null) {
                dto.setDelegateRemark(caseInfo.getDelegateReamrk());
                dto.setLicenseNo(caseInfo.getLicenseNo());
                dto.setUserName(caseInfo.getUserName());
            }
            return dto;
        }
        return null;
    }

    @Override
    public void lossReassign(LossReassignRequest request) throws AppException {
        // 校验
        LogUtils.info("开始执行申请改派，改派任务ID：" + request.getLossUuids());
        validateService.validateLossReassign(request);
        LogUtils.info("校验完成，调用理赔核心");
        // TODO 会调用理赔接口
        // 更新查勘任务状态
        LogUtils.info("调用理赔完成，更新查勘任务的状态");
        String[] lossUuids = StringUtils.split(request.getLossUuids(), ",");
        List<PrpmLossItem> lossItemList = Lists.newArrayList();
        for (String lossUuid : lossUuids) {
            LogUtils.info("更新查勘任务的状态， 查勘任务ID:" + lossUuid);
            PrpmLossItem lossItem = prpmLossItemMapper.selectByPrimaryKey(lossUuid);
            if (lossItem == null) {
                throw new AppException(MobileReturnCodeSupport.CODE_ERR_AUTH, MobileReturnCodeSupport.CODE_ERROR_AUTH_LOSSUUID_SURVEY);
            }
            lossItem.setLossStatus(3);
            prpmLossItemMapper.updateByPrimaryKey(lossItem);
            lossItemList.add(lossItem);
        }
        // 增加改派表
        LogUtils.info("更新查勘任务的状态完成，开始增加申请改派表");
        for (PrpmLossItem lossItem : lossItemList) {
            PrpmCaseInfo caseInfo = mobileCaseService.getCaseInfoByCaseNo(lossItem.getCaseNo());
            PrpmLossReassign reassign = new PrpmLossReassign();
            reassign.setCaseNo(caseInfo.getCaseNo());
            reassign.setCaseUuid(caseInfo.getCaseInfoId());
            reassign.setLossId(lossItem.getLossId().toString());
            reassign.setLossType(lossItem.getLossType().toString());
            reassign.setLossUuid(lossItem.getLossItemId());
            reassign.setReassignMark(request.getApplyScheReason());
            reassign.setOperateTime(new Date());
            reassign.setReassignPlat(Integer.parseInt(request.getApplySchePlat()));
            reassign.setReassignType(Integer.parseInt(request.getApplyScheType()));
            prpmLossReassignMapper.insert(reassign);
        }
    }

    @Override
    public List<PrpmLossItem> getLossItemByCaseNo(String userCode, String caseNo, String caseUuid) {
    	Map<String,String> parm=new HashMap<String,String>();
    	parm.put("userCode", userCode);
    	parm.put("caseNo", caseNo);
    	parm.put("caseUuid", caseUuid);
        return prpmLossItemMapper.selectByCaseNo(parm);
    }

    @Override
    public List<PrpmLossItem> getLossItemListByphoneNo(String phoneNo) throws AppException {
        if (StringUtils.isEmpty(phoneNo)) {
            throw new AppException(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.CODE_PARAM_NO_PHONENO);
        }
        return prpmLossItemMapper.selectByPhoneNo(phoneNo);
    }

    @Override
    public List<PrpmLossItem> getLossListByUserCodeOrLastTwo(String userCode, String lastTwo,String source) {
        
        return prpmLossItemMapper.getLossListByUserCodeOrLastTwo(userCode, lastTwo,source);
    }
}
